package com.nandi.deviceinstall.presenter

import com.nandi.deviceinstall.base.BasePresenter
import com.nandi.deviceinstall.bean.BindRecordInfoBean
import com.nandi.deviceinstall.bean.ListDataBean
import com.nandi.deviceinstall.contract.BindRecordInfoContract
import com.nandi.deviceinstall.contract.BindSuccessContract
import com.nandi.deviceinstall.model.BindRecordInfoModel
import com.nandi.deviceinstall.model.BindSuccessModel
import com.nandi.deviceinstall.net.BaseSubscriber
import com.nandi.deviceinstall.net.RxScheduler
import com.nandi.deviceinstall.net.dto.DataDTO
import com.nandi.deviceinstall.util.CustomToast
import com.uber.autodispose.FlowableSubscribeProxy
import org.reactivestreams.Subscription

class BindSuccessPresenter: BasePresenter<BindSuccessContract.View>(), BindSuccessContract.Presenter {
    override fun requestData(token: String, devId: String) {
        if (!isViewAttached) {
            return
        }
        mView!!.showLoading()
        model.requestData(token,devId)
                .compose(RxScheduler.Flo_io_main())
                .`as`<FlowableSubscribeProxy<ListDataBean>>(mView!!.bindAutoDispose())
                .subscribe(object : BaseSubscriber<ListDataBean>(mView!!) {
                    override fun onNext(t: ListDataBean) {
                        mView!!.handlerDataResult(t)
                    }

                    override fun onSubscribe(s: Subscription?) {
                        s!!.request(1)
                    }

                    override fun onError(e: Throwable?) {
                        super.onError(e)

                    }
                })
    }


    private val model: BindSuccessContract.Model

    init {
        model = BindSuccessModel()
    }

    override fun requestBindRecordInfo(token: String, id: Int) {
//View是否绑定 如果没有绑定，就不执行网络请求
        if (!isViewAttached) {
            return
        }
        mView!!.showLoading()
        model.executeBindRecordInfo(token,id)
                .compose(RxScheduler.Flo_io_main())
                .`as`<FlowableSubscribeProxy<BindRecordInfoBean>>(mView!!.bindAutoDispose())
                .subscribe(object : BaseSubscriber<BindRecordInfoBean>(mView!!) {
                    override fun onNext(t: BindRecordInfoBean) {
                        mView!!.handlerBindRecordResult(t)
                    }

                    override fun onSubscribe(s: Subscription?) {
                        s!!.request(1)
                    }

                    override fun onError(e: Throwable?) {
                        super.onError(e)
                    }
                })
    }
}